//
//  WBWebViewController.m
//  WBWebViewDemo
//
//  Created by 吴飚 on 2018/3/16.
//  Copyright © 2018年 canpoint. All rights reserved.
//

#import "WBWebViewController.h"
#import "WBWebView.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface WBWebViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) WBWebView *webView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation WBWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *htmlString = @"<p>一组漫画告诉孩子，为什么要努力读书<br/><img src=\"https://imgs9.canpoint.net/zone/23766d08021e4c25214a35c79a895c6515088140445009680\" title=\"23766d08021e4c25214a35c79a895c6515088140445009680\"/><br/><img src=\"https://imgs9.canpoint.net/zone/754638a937793108b81bb2d1a835b8d115088140578816196\" title=\"754638a937793108b81bb2d1a835b8d115088140578816196\"/><br/><img src=\"https://imgs9.canpoint.net/zone/bb42afada6f43c957a6985943cee7cca15088140710118513\" title=\"bb42afada6f43c957a6985943cee7cca15088140710118513\"/><br/><img src=\"https://imgs9.canpoint.net/zone/89385b71976807f749e57f7ff622abd715088140607947009\" title=\"89385b71976807f749e57f7ff622abd715088140607947009\"/><br/><img src=\"https://imgs9.canpoint.net/zone/1be0313543a4e143c0a4abde80087f8015088140708689476\" title=\"1be0313543a4e143c0a4abde80087f8015088140708689476\"/><br/><img src=\"https://imgs9.canpoint.net/zone/62d116a27baaaa8b0a568742173c3f2015088140805213188\" title=\"62d116a27baaaa8b0a568742173c3f2015088140805213188\"/><br/>看完这组漫画，您有什么想法？是不是更加有动力学习了<img src=\"https://imgs9.canpoint.net/emotion/images/qpx/q_0007.gif\"/></p>";
    
    _dataSource = [[NSMutableArray alloc] init];
    for (int i = 0; i < 20; i++)
    {
        NSString *item = [NSString stringWithFormat:@"第%d行",i];
        [_dataSource addObject:item];
    }
    
    _webView = [[WBWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    _webView.scrollView.scrollEnabled = NO;
    _webView.webDelegate = (id)self;
    _webView.opaque = NO;
    _webView.scrollView.bounces = NO;
    _webView.backgroundColor = [UIColor clearColor];
    _webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
    [_webView loadHTMLString:[NSString stringWithFormat:@"<meta name=\"viewport\" content=\"height=device-height,width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no\" />%@<div id=\"testDiv\" style = \"height:10px; width:100px\"></div>",htmlString] baseURL:nil];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _webView;
    [self.view addSubview:_tableView];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [webView evaluateJavaScript:@"document.getElementById(\"testDiv\").offsetTop"completionHandler:^(id _Nullable result,NSError * _Nullable error) {
        //获取页面高度，并重置webview的frame
        CGFloat lastHeight  = [result doubleValue];
        webView.frame = CGRectMake(0, 0, kScreenWidth, lastHeight);
        [self.tableView beginUpdates];
        [self.tableView endUpdates];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = _dataSource[indexPath.row];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
